package cn.appsys.service.impl;

import cn.appsys.mapper.VersionMapper;
import cn.appsys.pojo.Version;
import cn.appsys.service.VersionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;
@Service
@Transactional(rollbackFor = Exception.class)
public class VersionServiceImpl implements VersionService {

    @Resource
    VersionMapper versionMapper;

    @Override
    public List<Version> getVersionListByAppId(Long id) {
        return versionMapper.getVersionListByAppId(id);
    }

    @Override
    public Version getVersionByAppId(Long id) {
        return versionMapper.getVersionByAppId(id);
    }

    @Override
    public Version getVersionById(Long id) {
        return versionMapper.getVersionById(id);
    }

    @Override
    public Integer addVersion(Version version) {
        return versionMapper.addVersion(version);
    }

    @Override
    public Integer updateVersion(Version version) {
        return versionMapper.updateVersion(version);
    }

    @Override
    public Integer delApk(Long id) throws Exception {
        Version versionById = getVersionById(id);
        Integer integer = versionMapper.delApk(id);
        if (integer > 0) {
            new File(versionById.getDownloadLink()).delete();
        }
        return integer;
    }

    @Override
    public Integer delVersionByAppId(Long appId) {
        return versionMapper.delVersionByAppId(appId);
    }
}
